﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Collections.Generic;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using BLL;
using IFactory;
using System.IO;

public partial class QuarterPlan_QuarterPlanDisplay : System.Web.UI.Page
{
    DepartmentInfoManager dllDepartmentInfoManager = new DepartmentInfoManager();
    QuarterDateManager dllQuarterDateManager = new QuarterDateManager();
    QuarterPlanofManager dllQuarterPlanofManager = new QuarterPlanofManager();
    UserInfoManager dllUserInfoManager = new UserInfoManager();
    QuarterPlanofManager dllQuarterPlan = new QuarterPlanofManager();
    IBussinessLogic dllQuarterPlanDetail = new QuarterPlanDetailManager();
    IBussinessLogic dllUserInfo = new UserInfoManager();
    Authentication dllAuthentication = new Authentication();
    DepartmentInfoManager dllDepartment = new DepartmentInfoManager();
    Model.UserInfo objUserInfo = new Model.UserInfo();
    DataTable _powerTable = new DataTable();
    protected void Page_Load(object sender, EventArgs e)
    {
        dllAuthentication.isLogin();
        this.btnModify.Enabled = false;
        if (objUserInfo.UserName == "admin")
        {
            this.btnModify.Enabled = true;
        }

        //判断是否为经理，非经理锁定部门选择框
        this.drpDepartment.Enabled = false;
        int _isManager = dllAuthentication.IsManager(objUserInfo.UserName);
        if (_isManager > 0)
        {
            this.drpDepartment.Enabled = true;
        }


        //得到年度计划信息  
        if (!IsPostBack)
        {
            bindDropDownList(this.drpDepartment);
            bindDropDownList(this.drpYear);

            string dp = "请选择部门";
            if (Request.Params["depart"] != null && Request.Params["depart"].ToString().Length != 0 && Request.Params["depart"].ToString() != "请选择部门")
            {
                string did = Request.Params["depart"].ToString();
                this.drpDepartment.SelectedValue = did;
            }
            if (Request.Params["department"] != null && Request.Params["department"].ToString().Length != 0 && Request.Params["department"].ToString() != "请选择部门")
            {
                string department = Server.UrlDecode(Request.Params["department"].ToString());
                this.drpDepartment.SelectedIndex = this.drpDepartment.Items.IndexOf(this.drpDepartment.Items.FindByText(department));
            }
            if (Request.Params["Year"] != null && Request.Params["Year"].ToString().Length != 0 && Request.Params["Year"].ToString() != "请选择部门")
            {
                string Year = Server.UrlDecode(Request.Params["Year"].ToString());
                this.drpYear.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByValue(Year));
            }
            bindDropDownList(this.drpQuarterDate);
            if (Request.Params["Quarter"] != null && Request.Params["Quarter"].ToString().Length != 0 && Request.Params["Quarter"].ToString() != "请选择部门")
            {
                string Quarter = Server.UrlDecode(Request.Params["Quarter"].ToString());
                this.drpQuarterDate.SelectedIndex = this.drpQuarterDate.Items.IndexOf(this.drpQuarterDate.Items.FindByValue(Quarter));
            }
            //绑定列表                           
            bindGridView();

        }
        process();

    }

    //构建上年计划表结构并添加1行
    DataTable createDataTable()
    {
        DataTable _dt = new DataTable();
        DataColumn dc = new DataColumn("QuarterPlanSortId");
        _dt.Columns.Add(dc);
        dc = new DataColumn("WorkItem");
        _dt.Columns.Add(dc);
        dc = new DataColumn("Source");
        _dt.Columns.Add(dc);
        dc = new DataColumn("CompleteTime");
        _dt.Columns.Add(dc);
        dc = new DataColumn("MasterPeople");
        _dt.Columns.Add(dc);
        dc = new DataColumn("SlaveDepartment");
        _dt.Columns.Add(dc);
        dc = new DataColumn("SlaveDepartmentDuty");
        _dt.Columns.Add(dc);
        dc = new DataColumn("EvaluatePeople");
        _dt.Columns.Add(dc);
        dc = new DataColumn("PlanSendState");
        _dt.Columns.Add(dc);
        dc = new DataColumn("CheckNote");
        _dt.Columns.Add(dc);
        dc = new DataColumn("Remart");
        _dt.Columns.Add(dc);
        dc = new DataColumn("QuarterPlanId");
        _dt.Columns.Add(dc);
        dc = new DataColumn("StartTime");
        _dt.Columns.Add(dc);
        dc = new DataColumn("NoCompleteCount");
        _dt.Columns.Add(dc);
        dc = new DataColumn("VerifyPeopleOpinion");
        _dt.Columns.Add(dc);
        DataRow dr = _dt.NewRow();
        dr["QuarterPlanSortId"] = "";
        dr["WorkItem"] = "";
        dr["Source"] = "";
        dr["CompleteTime"] = "";
        dr["MasterPeople"] = "";
        dr["SlaveDepartment"] = "";
        dr["SlaveDepartmentDuty"] = "";
        dr["EvaluatePeople"] = "";
        dr["PlanSendState"] = "0";
        dr["CheckNote"] = "";
        dr["Remart"] = "";
        dr["QuarterPlanId"] = "";
        dr["StartTime"] = "";
        dr["NoCompleteCount"] = "";
        dr["VerifyPeopleOpinion"] = "";
        _dt.Rows.Add(dr);
        return _dt;
    }

    /// <summary>
    /// 根据逗号换行事件
    /// </summary>
    void wrapByComma(GridView _gridView, int _rowIndex, int _cells)
    {
        string _content = _gridView.Rows[_rowIndex].Cells[_cells].Text;
        _gridView.Rows[_rowIndex].Cells[_cells].Text = _content.Replace("&lt;br&gt;", "<br>");
        //_gridView.Rows[_rowIndex].Cells[_cells].Text = _content.Replace("&gt;", ">");
        _gridView.Rows[_rowIndex].Cells[_cells].Text = _gridView.Rows[_rowIndex].Cells[_cells].Text.Replace(",", "<br>");


    }

    /// <summary>
    /// 绑定gridview事件
    /// </summary>
    void bindGridView()
    {
        //得到用户名
        this.gvlist.DataSource = new DataTable();
        this.gvlist.DataBind();
        Model.UserInfo _objUserInfo = new Model.UserInfo();
        object[] _value = new object[3];
        _value[0] = Convert.ToInt32(this.drpQuarterDate.SelectedValue);
        _value[1] = _objUserInfo.UserName;
        _value[2] = this.drpDepartment.SelectedItem.Text;
        DataSet _quarterPlan = dllQuarterPlan.getDataSet(QuarterPlanofManager.field.MasterDepartment.ToString(), _value);
        if (_quarterPlan.Tables.Count > 1)
        {
            for (int j = 1; j < _quarterPlan.Tables.Count; j++)
            {
                if (_quarterPlan.Tables[j].Rows.Count > 0)
                {
                    DataView dv = new DataView(_quarterPlan.Tables[j]);
                    dv.Sort = "QuarterPlanSortId";
                    this.gvlist.DataSource = dv;
                    this.gvlist.DataBind();
                }
            }
        }
        if (this.gvlist.Rows.Count == 0)
        {
            this.gvlist.DataSource = createDataTable();
            this.gvlist.DataBind();
        }
        process();

        object[] _valueSummary = new object[2];
        _valueSummary[0] = Convert.ToInt32(this.drpQuarterDate.SelectedValue);
        _valueSummary[1] = this.drpDepartment.SelectedItem.Text;
        DataTable dt = new BLL.QuarterSummary().get(BLL.QuarterSummary.field.QuarterDate.ToString(), BLL.QuarterSummary.field.Department.ToString(), _valueSummary);
        if (dt.Rows.Count > 0)
        {
            this.txtQuarterSummary.Text = dt.Rows[0]["QuarterSummary"].ToString();
            this.txtPreQuarterSummary.Text = dt.Rows[0]["PreQuarterSummary"].ToString();
            this.prelength.InnerHtml = this.txtPreQuarterSummary.Text.Length.ToString();
            this.thelength.InnerHtml = this.txtQuarterSummary.Text.Length.ToString();
            this.HQuarterSummaryId.Value = dt.Rows[0]["Id"].ToString();
            this.HPreAttachmentPath.Value = dt.Rows[0]["PreAttachmentPath"].ToString();
            this.HAttachmentPath.Value = dt.Rows[0]["AttachmentPath"].ToString();
        }
        else
        {
            this.txtQuarterSummary.Text = "注：工作部署不少于3条，总字数不少于60个字。";
            this.txtPreQuarterSummary.Text = "注：工作总结不少于3条，总字数不少于60个字。";
            //this.lblPreQuarterSummary.Text = "注：工作概述和总结不少于3条，每条字数不少于20个字。";
            //this.lblQuarterSummary.Text = "注：工作概述和总结不少于3条，每条字数不少于20个字。";
            this.prelength.InnerHtml = "0";
            this.thelength.InnerHtml = "0";
            this.HQuarterSummaryId.Value = string.Empty;
            this.HPreAttachmentPath.Value = string.Empty;
            this.HAttachmentPath.Value = string.Empty;
        }
        if (this.txtQuarterSummary.Text.Length > 0 && this.txtQuarterSummary.Text != "注：工作部署不少于3条，总字数不少于60个字。")
        {
            this.btnAddQuarterSummary.Text = "修改";
        }
        else
        {
            this.btnAddQuarterSummary.Text = "添加";
        }
        if (this.txtPreQuarterSummary.Text.Length > 0 && this.txtPreQuarterSummary.Text != "注：工作总结不少于3条，总字数不少于60个字。")
        {
            this.btnAddPreQuarterSummary.Text = "修改";
        }
        else
        {
            this.btnAddPreQuarterSummary.Text = "添加";
        }
        if (this.HPreAttachmentPath.Value.Length > 0)
        {
            this.btnDownPreQuarter.Enabled = true;
        }
        else
        {
            this.btnDownPreQuarter.Enabled = false;
        }
        if (this.HAttachmentPath.Value.Length > 0)
        {
            this.btnDownQuarterSummary.Enabled = true;
        }
        else
        {
            this.btnDownQuarterSummary.Enabled = false;
        }
        if (objUserInfo.UserName != "admin")
        {
            bool Isdepartment = false;
            //当前部门并且当前月份
            object[] _value1 = new object[1];
            _value1[0] = objUserInfo.UserName;
            DataTable _userTable = new BLL.UserInfoManager().get(UserInfoManager.field.UserName.ToString(), _value1);
            if (_userTable.Rows.Count > 0)
            {
                if (_userTable.Rows[0]["DepartmentId"].ToString() == this.drpDepartment.SelectedItem.Text || _userTable.Rows[0]["PartTimeDepartmentId"].ToString() == this.drpDepartment.SelectedItem.Text)
                {
                    Isdepartment = true;
                }
            }

            //bool IsDate = false;
            //DataTable _quartertime = new BLL.QuarterDateManager().getQuarterDate();

            //if (this.drpQuarterDate.SelectedItem.Text == _quartertime.Rows[0][1].ToString())
            //{
            //    IsDate = true;

            //}
            if (Isdepartment)
            {

                this.btnAddPreQuarterSummary.Enabled = true;
                this.btnAddQuarterSummary.Enabled = true;
                this.btnUploadPreQuarterSummary.Enabled = true;
                this.btnUploadQuarterSummary.Enabled = true;
            }
            else
            {
                this.btnAddPreQuarterSummary.Enabled = false;
                this.btnAddQuarterSummary.Enabled = false;
                this.btnUploadPreQuarterSummary.Enabled = false;
                this.btnUploadQuarterSummary.Enabled = false;
            }
            this.btnSaveQuarterSummary.Visible = false;
            this.btnSavePreQuarterSummary.Visible = false;
            this.btnAddQuarterSummary.Visible = true;
            this.btnAddPreQuarterSummary.Visible = true;
        }
        this.txtQuarterSummary.ReadOnly = true;
        this.txtPreQuarterSummary.ReadOnly = true;
    }

    void process()
    {
        #region    截取字符串处理，是否显示查看年分解按钮
        for (int i = 0; i < this.gvlist.Rows.Count; i++)
        {
            object[] _valueQuarterPlanResolve = new object[1];
            _valueQuarterPlanResolve[0] = this.gvlist.DataKeys[i]["QuarterPlanId"].ToString();
            DataTable _quarterResolve = dllQuarterPlanDetail.get(QuarterPlanDetailManager.field.QuarterPlanId.ToString(), _valueQuarterPlanResolve);
            if (_quarterResolve.Rows.Count > 0)
            {
                Button btn = (Button)this.gvlist.Rows[i].Cells[11].FindControl("btnQuarterPlanResolve");
                Label lbl = (Label)this.gvlist.Rows[i].Cells[11].FindControl("lblTopic");
                btn.Visible = true;
                lbl.Visible = false;
            }
            else
            {
                Button btn = (Button)this.gvlist.Rows[i].Cells[11].FindControl("btnQuarterPlanResolve");
                Label lbl = (Label)this.gvlist.Rows[i].Cells[11].FindControl("lblTopic");
                btn.Visible = false;
                lbl.Visible = true;
            }
            wrapByComma(this.gvlist, i, 6);
            wrapByComma(this.gvlist, i, 7);
            if (this.gvlist.Rows[i].Cells[10].Text != "")
            {
                if (Convert.ToInt32(this.gvlist.DataKeys[i]["PlanSendState"].ToString()) == 4)
                {
                    this.gvlist.Rows[i].BackColor = System.Drawing.Color.FromName("#00C0C0");
                }
                if (Convert.ToInt32(this.gvlist.DataKeys[i]["PlanSendState"].ToString()) == 5)
                {
                    this.gvlist.Rows[i].BackColor = System.Drawing.Color.FromName("#00C000");
                }
                if (Convert.ToInt32(this.gvlist.DataKeys[i]["PlanSendState"].ToString()) == 6)
                {
                    this.gvlist.Rows[i].BackColor = System.Drawing.Color.Yellow;
                }
                if (Convert.ToInt32(this.gvlist.DataKeys[i]["PlanSendState"].ToString()) == 7)
                {
                    this.gvlist.Rows[i].BackColor = System.Drawing.Color.Red;
                }
                if (Convert.ToInt32(this.gvlist.DataKeys[i]["PlanSendState"].ToString()) == 8)
                {
                    this.gvlist.Rows[i].BackColor = System.Drawing.Color.Gray;
                }
                if (Convert.ToInt32(this.gvlist.DataKeys[i]["PlanSendState"].ToString()) == 1 || Convert.ToInt32(this.gvlist.DataKeys[i]["PlanSendState"].ToString()) == 2)
                {
                    this.gvlist.Rows[i].BackColor = System.Drawing.Color.Pink;
                }

                //if (Convert.ToInt32(this.gvlist.DataKeys[i]["PlanSendState"].ToString()) > 2)
                //{
                //    this.gvlist.Rows[i].Cells[10].Text = "达成共识";
                //}
                //else
                //{
                //    this.gvlist.Rows[i].Cells[10].Text = "";
                //}

            }

        }
        #endregion
    }




    /// <summary>
    /// 绑定DropDownList
    /// </summary>
    /// <author>grokyao</author>
    /// <date>2009-2-16</date>
    void bindDropDownList(DropDownList _drp)
    {
        if (_drp.Equals(this.drpDepartment))
        {
            this.drpDepartment.DataSource = dllDepartment.getNoTree();
            this.drpDepartment.DataTextField = "DepartmentName";
            this.drpDepartment.DataValueField = "DepartmentId";
            this.drpDepartment.DataBind();
            ListItem _li = new ListItem("请选择部门");
            this.drpDepartment.Items.Insert(0, _li);

            object[] _value = new object[1];
            _value[0] = objUserInfo.UserName;
            DataTable _userTable = dllUserInfoManager.get(UserInfoManager.field.UserName.ToString(), _value);
            if (_userTable.Rows.Count > 0)
            {
                this.drpDepartment.SelectedIndex = this.drpDepartment.Items.IndexOf(this.drpDepartment.Items.FindByText(_userTable.Rows[0]["DepartmentId"].ToString()));
            }
        }
        if (_drp.Equals(this.drpYear))
        {
            for (int i = 2009; i < 2100; i++)
            {
                ListItem li = new ListItem(i.ToString(), i.ToString());
                _drp.Items.Add(li);
            }
            //根据系统日期自动绑定到当前年份
            //string _year = DateTime.Now.Year.ToString();
            //this.drpYear.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByText(_year));
            //根据系统当前日期查找到对应的年周期
            DataTable _year = dllQuarterDateManager.getQuarterDate();
            this.drpYear.SelectedIndex = this.drpYear.Items.IndexOf(this.drpYear.Items.FindByText(_year.Rows[0][2].ToString()));

        }
        if (_drp.Equals(this.drpQuarterDate))
        {
            object[] _value = new object[1];
            _value[0] = this.drpYear.SelectedValue;
            DataTable _quarter = dllQuarterDateManager.get(QuarterDateManager.field.QuarterYear.ToString(), _value);
            if (_quarter.Rows.Count != 0)
            {
                this.drpQuarterDate.DataSource = _quarter;
                this.drpQuarterDate.DataTextField = "QuarterName";
                this.drpQuarterDate.DataValueField = "Quarter_Id";
                this.drpQuarterDate.DataBind();
                #region dropdownlist显示系统所对应的当前年周期
                //根据系统当前日期查找到对应的年周期
                DataTable _quartertime = dllQuarterDateManager.getQuarterDate();
                //如果查到结果（结果只有一条）

                if (_quartertime.Rows.Count == 1)
                {
                    this.drpQuarterDate.SelectedIndex = this.drpQuarterDate.Items.IndexOf(this.drpQuarterDate.Items.FindByText(_quartertime.Rows[0][1].ToString()));
                    this.drpQuarterDate.DataSource = _quartertime;
                    this.drpQuarterDate.DataTextField = "QuarterName";
                    this.drpQuarterDate.DataValueField = "Quarter_Id";
                }
                #endregion

            }
            else
            {
                ListItem li = new ListItem("无年周期", "0");
                this.drpQuarterDate.Items.Clear();
                this.drpQuarterDate.Items.Add(li);
            }

        }
    }




    /// <summary>
    /// 分页事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void gvlist_PageIndexChanging(object sender, GridViewPageEventArgs e)
    {
        gvlist.PageIndex = e.NewPageIndex;

        bindGridView();

        this.gdvQuarterResolve.Visible = false;
    }


    /// <summary>
    /// 绑定年分解计划GridView
    /// </summary>
    void bindGridViewQuarterResolve(string _quarterPlanId)
    {
        object[] _value = new object[1];
        _value[0] = _quarterPlanId;
        DataTable _quarterResolve = dllQuarterPlanDetail.get(QuarterPlanDetailManager.field.QuarterPlanId.ToString(), _value);
        this.gdvQuarterResolve.DataSource = _quarterResolve;
        this.gdvQuarterResolve.DataBind();
        for (int i = 0; i < this.gdvQuarterResolve.Rows.Count; i++)
        {
            string _Lengthstart = this.gdvQuarterResolve.Rows[i].Cells[1].Text;
            string _Lengthend = this.gdvQuarterResolve.Rows[i].Cells[2].Text;

            if (_Lengthstart.Length > 9)
            {
                _Lengthstart = _Lengthstart.Substring(0, 9);//截取字符串，从左边截取10个字符串
                this.gdvQuarterResolve.Rows[i].Cells[1].Text = _Lengthstart;
            }
            if (_Lengthend.Length > 9)
            {
                _Lengthend = _Lengthend.Substring(0, 9);//截取字符串，从左边截取10个字符串
                this.gdvQuarterResolve.Rows[i].Cells[2].Text = _Lengthend;
            }
            wrapByComma(this.gdvQuarterResolve, i, 5);
            wrapByComma(this.gdvQuarterResolve, i, 6);

        }
    }
    /// <summary>
    /// 查看年分解计划事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnQuarterPlanResolve_Click(object sender, EventArgs e)
    {
        int i = 0;
        Button bt = (Button)sender;//获得当前控件
        TableCell cell = (TableCell)bt.Parent;//得到所处的父单元格
        GridViewRow iRow = (GridViewRow)cell.Parent;//得到所处父单元行
        i = iRow.RowIndex;//得到该行的索引
        int _quarterPlanId = Convert.ToInt32(this.gvlist.DataKeys[i]["QuarterPlanId"].ToString());
        Utility.HttpHelp.windowOpen("QuarterPlanResolveDetail.aspx?_quarterPlanId=" + _quarterPlanId, Page);

    }


    /// <summary>
    /// DropDownList索引改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <date>2009-3-6</date>
    protected void drpDepartment_SelectedIndexChanged(object sender, EventArgs e)
    {
        ListItem _li = this.drpDepartment.SelectedItem;
        if (_li != null && _li.Text != "请选择部门")
        {
            bindGridView();
        }
        this.btnAddQuarterSummary.Visible = true;
        this.btnAddPreQuarterSummary.Visible = true;
        this.btnSaveQuarterSummary.Visible = false;
        this.btnSavePreQuarterSummary.Visible = false;
    }


    /// <summary>
    /// 修改按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    /// <author>grokyao</author>
    /// <Date>2009-3-5</Date>
    protected void btnModify_Click(object sender, EventArgs e)
    {
        //得到部门的信息
        string department = drpDepartment.SelectedItem.Value;

        int _count = 0;
        int _quarterPlanId = 0;
        for (int i = 0; i < this.gvlist.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gvlist.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked == true)
            {
                if (this.gvlist.DataKeys[i]["QuarterPlanId"] != null && this.gvlist.DataKeys[i]["QuarterPlanId"].ToString().Length != 0)
                {

                    _count++;
                    _quarterPlanId = Convert.ToInt32(this.gvlist.DataKeys[i]["QuarterPlanId"].ToString());
                }
            }
        }
        if (_count == 1)
        {  //判断是否为评估人，如果该条计划的评估人是当前用户则不能修改该条计划

            Model.UserInfo objUser = new Model.UserInfo();
            object[] _user = new object[1];
            _user[0] = objUser.UserName;
            DataTable _userTable = dllUserInfo.get(UserInfoManager.field.UserName.ToString(), _user);
            if (_userTable.Rows.Count > 0)
            {
                object[] _username = new object[2];
                _username[0] = _userTable.Rows[0]["RealName"].ToString();
                _username[1] = _quarterPlanId;
                DataTable _iSEvaluatePeople = dllQuarterPlan.get(QuarterPlanofManager.field.EvaluatePeople.ToString(), QuarterPlanofManager.field.QuarterPlanId.ToString(), _username);
                if (_iSEvaluatePeople.Rows.Count > 0)
                {
                    Utility.HttpHelp.alert("评估人不能修改计划", Page);
                }
                else
                {
                    string _url = "ModifyQuarterPlan.aspx?_quarterPlanId=" + _quarterPlanId + "&_quarterDate=" + Utility.HttpHelp.getParamsInt("Id") + "&depart=" + department + "&Year=" + this.drpYear.SelectedValue + "&Quarter=" + this.drpQuarterDate.SelectedValue;
                    Page.RegisterStartupScript("windowopen", "<script>window.open('" + _url + "','','height=200,width=1000,top=300,left=20,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');</script>");
                    return;
                }
            }

        }
        else
        {
            Utility.HttpHelp.alert("只能选择一个", Page);
        }
    }



    /// <summary>
    /// 删除
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnDel_Click(object sender, EventArgs e)
    {
        int _count = 0;
        List<object[]> _valueList = new List<object[]>();
        for (int i = 0; i < this.gvlist.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gvlist.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked)
            {
                if (this.gvlist.DataKeys[i]["QuarterPlanId"] != null && this.gvlist.DataKeys[i]["QuarterPlanId"].ToString().Length != 0)
                {

                    _count++;
                    object[] _value = new object[1];
                    _value[0] = Convert.ToInt32(this.gvlist.DataKeys[i]["QuarterPlanId"].ToString());
                    _valueList.Add(_value);
                }
            }
        }
        if (_count == 0)
        {
            Utility.HttpHelp.alert("请选择要删除的年计划", Page);
        }
        if (_count > 0)
        {
            int result = dllQuarterPlan.delWithTransaction(QuarterPlanofManager.field.QuarterPlanId.ToString(), _valueList);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("删除年计划未成功，请重试！");
            }
            else
            {

                //bindGridView(txtDepartment.Value);
                ListItem _li = this.drpDepartment.SelectedItem;
                if (_li != null)
                {
                    bindGridView();
                }
            }
        }
    }

    /// <summary>
    /// 添加计划事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnAdd_Click(object sender, EventArgs e)
    {
        string _url = "QuarterPlanAddAdmin.aspx";
        Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','quarterplandetailmodify','height=400,width=900,top=200,left=80,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
    }




    /// <summary>
    /// 查看流程
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnflow_Click(object sender, EventArgs e)
    {
        int _count = 0;
        int _quarterPlanId = 0;
        for (int i = 0; i < this.gvlist.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gvlist.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked == true)
            {
                if (this.gvlist.DataKeys[i]["QuarterPlanId"] != null && this.gvlist.DataKeys[i]["QuarterPlanId"].ToString().Length != 0)
                {

                    _count++;
                    _quarterPlanId = Convert.ToInt32(this.gvlist.DataKeys[i]["QuarterPlanId"].ToString());
                }
            }
        }
        if (_count == 1)
        {  //判断是否为评估人，如果该条计划的评估人是当前用户则不能修改该条计划

            string _url = "../WebCommon/VerifyCircruitDisplay.aspx?PlanId=" + _quarterPlanId + "&PlanType=1";
            Page.RegisterStartupScript("windowOpen",
               "<script language=javascript>window.open('" + _url + "','VerifyCircruitDisplay','height=150,width=550,top=250,left=300,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");


        }
        else
        {
            Utility.HttpHelp.alert("请选择一条计划", Page);
        }
    }


    /// <summary>
    /// 年份改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpYear_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindDropDownList(this.drpQuarterDate);
        bindGridView();
        this.btnAddQuarterSummary.Visible = true;
        this.btnAddPreQuarterSummary.Visible = true;
        this.btnSaveQuarterSummary.Visible = false;
        this.btnSavePreQuarterSummary.Visible = false;
    }



    /// <summary>
    /// 年周期改变事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void drpQuarterDate_SelectedIndexChanged(object sender, EventArgs e)
    {
        bindGridView();
        this.btnAddQuarterSummary.Visible = true;
        this.btnAddPreQuarterSummary.Visible = true;
        this.btnSaveQuarterSummary.Visible = false;
        this.btnSavePreQuarterSummary.Visible = false;
    }


    /// <summary>
    /// 修改评估事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnModifyEvaluate_Click(object sender, EventArgs e)
    {
        int _count = 0;
        string _quarterPlanId = "";
        int _noComplete = 0;
        for (int i = 0; i < this.gvlist.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gvlist.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked)
            {
                _count++;
                _quarterPlanId = this.gvlist.DataKeys[i]["QuarterPlanId"].ToString();
                _noComplete = int.Parse(this.gvlist.DataKeys[i]["NoCompleteCount"].ToString());
            }
        }
        if (_count == 0)
        {
            Utility.HttpHelp.alert("请选择一条计划", Page);
        }
        if (_count > 1)
        {
            Utility.HttpHelp.alert("只能选择一条计划", Page);
        }
        if (_count == 1)
        {
            //if (objUserInfo.UserName == "admin" && _noComplete > 2)
            //{
            //    Utility.HttpHelp.alert("不可以修改多次未完成的计划评估意见", Page);
            //}
            //else
            //{
            string _department = Server.UrlEncode(this.drpDepartment.SelectedItem.Text);
            string _url = "ModifyEvaluate.aspx?QuarterPlanId=" + _quarterPlanId + "&department=" + _department + "&Date=" + this.drpQuarterDate.SelectedValue;
            Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','monthPlan','height=250,width=900,top=200,left=50,toolbar=no,menubar=no,scrollbars=yes,resizable=no,location=no,status=no');</script>");
            //}
        }
    }


    /// <summary>
    /// 导出execl
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnOutToExcel_Click(object sender, EventArgs e)
    {
        GridView[] _gridView = new GridView[1];
        _gridView[0] = this.gvlist;
        Utility.Comm.QuarterDisplayToExcel(GridViewToDataTable(_gridView)[0], this.drpDepartment.SelectedItem.Text, this.drpQuarterDate.SelectedItem.Text);
    }

    /// <summary>
    /// GridView转DataTable
    /// </summary>
    DataTable[] GridViewToDataTable(GridView[] _sourceGridView)
    {
        DataTable[] _targetDataTable = new DataTable[_sourceGridView.Length];
        for (int i = 0; i < _targetDataTable.Length; i++)
        {
            _targetDataTable[i] = new DataTable();
            DataColumn dc = new DataColumn();
            //根据源GridView循环创建目标DataTable列
            for (int j = 0; j < _sourceGridView[i].Columns.Count; j++)
            {
                if (_sourceGridView[i].Columns[j].Visible == true)
                {
                    dc = new DataColumn(_sourceGridView[i].Columns[j].HeaderText);
                    _targetDataTable[i].Columns.Add(dc);
                }
            }
            //根据源GridView循环创建目标DataTable行
            for (int j = 0; j < _sourceGridView[i].Rows.Count; j++)
            {
                DataRow dr = _targetDataTable[i].NewRow();
                for (int k = 0; k < _sourceGridView[i].Columns.Count; k++)
                {

                    if (_sourceGridView[i].Columns[k].Visible == true && k < _targetDataTable[i].Columns.Count)
                    {

                        dr[k] = _sourceGridView[i].Rows[j].Cells[k].Text;


                    }

                }
                _targetDataTable[i].Rows.Add(dr);
            }

        }
        return _targetDataTable;
    }



    /// <summary>
    /// 取消评估按钮事件
    /// </summary>
    /// <param name="sender"></param>
    /// <param name="e"></param>
    protected void btnCancelEvalation_Click(object sender, EventArgs e)
    {
        int _count = 0;
        List<object[]> _valueList = new List<object[]>();
        for (int i = 0; i < this.gvlist.Rows.Count; i++)
        {
            CheckBox ckb = (CheckBox)this.gvlist.Rows[i].Cells[0].FindControl("ckbSelect");
            if (ckb.Checked)
            {
                if (this.gvlist.DataKeys[i]["QuarterPlanId"] != null && this.gvlist.DataKeys[i]["QuarterPlanId"].ToString().Length != 0)
                {

                    _count++;
                    object[] _value = new object[1];
                    _value[0] = Convert.ToInt32(this.gvlist.DataKeys[i]["QuarterPlanId"].ToString());
                    _valueList.Add(_value);
                }
            }
        }
        if (_count == 0)
        {
            Utility.HttpHelp.alert("请选择要取消评估的年计划", Page);
        }
        if (_count > 0)
        {
            int result = dllQuarterPlan.editWithTransaction(QuarterPlanofManager.field.QuarterPlanId.ToString(), _valueList);
            if (result == 0)
            {
                Utility.Error.RedirectErrorPage("取消评估年计划未成功，请重试！");
            }
            else
            {
                ListItem _li = this.drpDepartment.SelectedItem;
                if (_li != null)
                {
                    bindGridView();
                }
            }
        }
    }



    protected void btnAddPreQuarterSummary_Click(object sender, EventArgs e)
    {
        this.btnSavePreQuarterSummary.Visible = true;
        this.btnAddPreQuarterSummary.Visible = false;
        this.txtPreQuarterSummary.ReadOnly = false;
        if (this.txtPreQuarterSummary.Text == "注：工作总结不少于3条，总字数不少于60个字。")
        {
            this.txtPreQuarterSummary.Text = "";
        }
        //this.lblPreQuarterSummary.Visible = false;
    }
    protected void btnAddQuarterSummary_Click(object sender, EventArgs e)
    {
        this.btnSaveQuarterSummary.Visible = true;
        this.btnAddQuarterSummary.Visible = false;
        this.txtQuarterSummary.ReadOnly = false;
        if (this.txtQuarterSummary.Text == "注：工作部署不少于3条，总字数不少于60个字。")
        {
            this.txtQuarterSummary.Text = "";
        }
        //this.lblQuarterSummary.Visible = false;
    }
    protected void btnSavePreQuarterSummary_Click(object sender, EventArgs e)
    {
        if (this.drpDepartment.SelectedItem.Text != "请选择部门")
        {
            if (this.HQuarterSummaryId.Value.Length > 0)
            {

                if (sender.Equals(this.btnSavePreQuarterSummary))
                {
                    if (this.txtPreQuarterSummary.Text.Length < 60)
                    {
                        Utility.HttpHelp.alert("字数不得少于60字！", this.Page);
                        return;
                    }
                }
                else
                {
                    if (this.txtQuarterSummary.Text.Length < 60)
                    {
                        Utility.HttpHelp.alert("字数不得少于60字！", this.Page);
                        return;
                    }
                }
                //修改
                object[] _value = new object[7];
                _value[0] = Convert.ToInt32(this.drpQuarterDate.SelectedValue);
                _value[1] = this.txtPreQuarterSummary.Text;
                _value[2] = this.txtQuarterSummary.Text;
                _value[3] = objUserInfo.UserName;
                BLL.UserInfoManager dllUserInfo = new BLL.UserInfoManager();
                DataTable dt = dllUserInfo.get(BLL.UserInfoManager.field.UserName.ToString(), new object[] { objUserInfo.UserName });
                _value[4] = Convert.ToInt32(dt.Rows[0]["UserId"]);
                _value[5] = this.drpDepartment.SelectedItem.Text;
                _value[6] = Convert.ToInt32(this.HQuarterSummaryId.Value);
                int val = new BLL.QuarterSummary().editQuarterSummary(_value);
                if (val > 0)
                {
                    bindGridView();
                }
                else
                {
                    Utility.HttpHelp.alert("修改失败", this.Page);
                }
            }
            else
            {
                //添加
                object[] _value = new object[5];
                _value[0] = Convert.ToInt32(this.drpQuarterDate.SelectedValue);
                _value[1] = this.txtPreQuarterSummary.Text;
                _value[2] = this.txtQuarterSummary.Text;
                _value[3] = objUserInfo.UserName;
                _value[4] = this.drpDepartment.SelectedItem.Text;
                int val = new BLL.QuarterSummary().add(_value);
                if (val > 0)
                {
                    bindGridView();
                }
                else
                {
                    Utility.HttpHelp.alert("添加失败", this.Page);
                }
            }
            if (sender.Equals(this.btnSaveQuarterSummary))
            {
                this.btnAddQuarterSummary.Visible = true;
                this.btnSaveQuarterSummary.Visible = false;
                this.txtQuarterSummary.ReadOnly = true;
                //this.lblQuarterSummary.Visible = true;
            }
            if (sender.Equals(this.btnSavePreQuarterSummary))
            {
                this.btnAddPreQuarterSummary.Visible = true;
                this.btnSavePreQuarterSummary.Visible = false;
                this.txtPreQuarterSummary.ReadOnly = true;
                //this.lblPreQuarterSummary.Visible = true;
            }
        }
        else
        {
            Utility.HttpHelp.alert("请选择部门", this.Page);
        }
    }
    protected void btnUpload_Click(object sender, EventArgs e)
    {
        if (this.HQuarterSummaryId.Value.Length > 0)
        {
            if (sender.Equals(this.btnUploadQuarterSummary))
            {
                //定向到添加本月附件路径页面
                string _url = "../WebCommon/AttachmentPath.aspx?PlanId=" + this.HQuarterSummaryId.Value + "&PlanType=15&Url=QuarterPlanDisplay.aspx";
                Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','quarterplandetailmodify','height=40,width=400,top=300,left=300,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');</script>");
            }
            else
            {
                //定向到添加上月附件路径页面
                string _url = "../WebCommon/AttachmentPath.aspx?PlanId=" + this.HQuarterSummaryId.Value + "&PlanType=12&Url=QuarterPlanDisplay.aspx";
                Page.RegisterStartupScript("windowsopen", "<script language=javascript>window.open('" + _url + "','quarterplandetailmodify','height=40,width=400,top=300,left=300,toolbar=no,menubar=no,scrollbars=no,resizable=no,location=no,status=no');</script>");
            }
        }
        else
        {
            if (sender.Equals(this.btnUploadQuarterSummary))
            {
                Utility.HttpHelp.alert("请先添加本年总结", this.Page);
            }
            else
            {
                Utility.HttpHelp.alert("请先添加上年总结", this.Page);
            }

        }
    }
    protected void btnDownQuarterSummary_Click(object sender, EventArgs e)
    {
        if (sender.Equals(this.btnDownQuarterSummary))
        {
            string path = this.HAttachmentPath.Value;
            FileDownload(path);
        }
        else
        {
            string path = this.HPreAttachmentPath.Value;
            FileDownload(path);
        }
    }


    ///   <summary>   
    ///   文件下载   
    ///   </summary>   
    ///   <param   name="FullFileName"></param>   
    private void FileDownload(string strFileName)
    {

        FileInfo DownloadFile = new FileInfo(strFileName);
        if (!DownloadFile.Exists)
        {
            Utility.HttpHelp.alert("无附件,请与管理员联系", Page);
            return;
        }
        Response.Clear();
        Response.ClearHeaders();
        Response.Buffer = false;
        Response.ContentType = "application/octet-stream";
        Response.AppendHeader("Content-Disposition", "attachment;filename=" + HttpUtility.UrlEncode(strFileName.Substring(strFileName.LastIndexOf('\\') + 1), System.Text.Encoding.UTF8));
        // Response.AppendHeader("Content-Length", DownloadFile.Length.ToString());

        Response.WriteFile(DownloadFile.FullName);
        Response.Flush();
        Response.End();

    }
}
